package com.aqie.easy.math;

/**
 * 9. https://leetcode-cn.com/problems/palindrome-number/
 * 1.翻转整数
 * 2. 考虑溢出
 * 3. byte - 1; short - 2; int - 4; long - 8; float - 4; double - 8; char - 2; boolean - 1
 */
public class Palindrome {
    /**
     * 反转一半数字
     * 2221  x 会比 r大(位数多)， 因为要求的是回文，所以这里不影响
     * @param x
     * @return
     */
    public boolean isPalindrome(int x) {
        if(x < 0 || ((x % 10 == 0) && x != 0)) return false;
        int r = 0;
        while(x > r){
            r = r * 10 + x % 10;
            x /= 10;
        }
        // 当为奇数时，依据while循环 r比 x 多一位
        return x == r || x == r / 10;
    }

    public static void main(String[] args) {
        int x = 12345;
        x = x / 10;
        System.out.println(x);
        System.out.println(reverseNum(12345));
        System.out.println(Integer.MAX_VALUE);       // 2^31 - 1; 四字节32位
    }

    public static int reverseNum(int x){
        int r = 0;
        while (x > 0){
            r = r * 10 + x % 10;
            x /= 10;
        }
        return r;
    }
}
